define(["jquery","bbcare","as/app/gms/gms.servers","as/app/gms/gms.ui"],function($,$bb,$servers,$ui){
	var event = {};

	/**
	 *  gms保存评估结果成功后的回调函数
	 */
	var ajaxGetScoreCallBack = function(data,info){
		$bb.openPage("gmsDetail",{"info":info});
	}
	/**
	 * 保存
	 */
	var onGmsSubmit = function(){
		var info = $(this).data("info");
        var params = {"ps":{}};
        params.type = info.type;
        params.userId = info.userId;
        params.ps.birthday = info.birthday;
        params.ps.pregnancDay = info.pregnancDay;
        params.ps.posture = $("#posture").val();
        params.ps.testTime = $("#testTime").val();
        params.ps.days = $servers.getDiff(info.birthday,params.ps.testTime,info.pregnancDay);
        params.ps.item = $(".gms-form input[name=result]:checked").val();
        params.ps.color = $(".gms-form input[name=result]:checked").attr("data-itemColor");
        params.ps.stage = $(".gms-form input[name=result]:checked").attr("data-stage");
        params.ps.guides = $("#guidance").val();
        params.ps.remark = $("#remark").val();
        if( !params.ps.posture ){
        	$bb.toastShow("请选择本次评估体位！");
        }else if( !params.ps.testTime ){
        	$bb.toastShow("请选择本次评估时间！");
        }else if( !params.ps.item ){
        	$bb.toastShow("请选择本次评估结果！");
        }else{
        	$servers.ajaxGetScore(params,ajaxGetScoreCallBack,info);
        }
	}

	/**
	 * 清空
	 */
	var onGmsEmpty = function(){
		 $("#guidance,#remark").val("");
	}

	/**
	 * 选中某个患者
	 */
	var onPatientSelect = function(){
		$("#gmsPatients").css({"display":"none"});
		$("#gmsWrap").html("");
		var patients = $(this).data("patients");
		var patientId = $(this).attr("data-patientId");
		var info = $servers.getPatientInfo(patients,patientId,"GMS");
		$ui.info({
			"id":"gmsWrap",
			"info":info
		});
		$ui.result({
			"id":"resultBox",
			"info":info
		});
		$("#testTime").unbind("change");
		$("#testTime").change(function(){
			var testTime = $(this).val();
			info.testTime = testTime;
			$ui.result({
				"id":"resultBox",
				"info":info,
			});
		});
		$(".gms-submit").unbind("click");
		$(".gms-submit").data({"info":info});
		$(".gms-submit").click(onGmsSubmit);
		$(".gms-empty").unbind("click");
		$(".gms-empty").click(onGmsEmpty);
	}

	/**
	 * 根据手机号码或姓名进行搜索成功后的回调函数
	 */
	var ajaxSearchCallBack = function(data){
		if( data.rows.length > 0 ){
			$ui.patients({
				"id":"gmsPatients",
				"list":data.rows
			});
			$("#gmsPatients").css({"display":"block"});
			$("#gmsPatients li").unbind("click");
			$("#gmsPatients li").data({"patients":data.rows})
			$("#gmsPatients li").click(onPatientSelect);
			if( data.rows.length == 1 ) $("#gmsPatients li").eq(0).click();
		}else{
			$bb.toastShow("您搜索的患者未建档，请先建档");
		}
	}

	/**
	 * 根据手机号码或姓名进行搜索
	 */
	event.onGmsSearch = function(){
		var text = $.trim( $(this).parent().prev().val() );
		var reg = /^1[3456789]\d{9}$/;
		var params = { "flag":2,"systemType":"02" };
		if( !reg.test(text) ){
			params.patientName = text;
		}else{
			params.phoneNum = text;
		}
		$servers.ajaxSearch(params,ajaxSearchCallBack);
	}

	return event;
});